package com.wzh.house.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.wzh.house.vo.AttrValue;
import com.wzh.house.vo.AttrKey;
import com.wzh.house.vo.Attrs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wzh.house.entity.HouseAttrKeyEntity;
import com.wzh.house.service.HouseAttrKeyService;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.R;



/**
 * 
 *
 * @author wzh
 * @email 1281204121@qq.com
 * @date 2022-03-22 19:04:16
 */
@RestController
@RequestMapping("house/houseattrkey")
public class HouseAttrKeyController {
    @Autowired
    private HouseAttrKeyService houseAttrKeyService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = houseAttrKeyService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{typeId}")
    public R info(@PathVariable("typeId") Integer typeId){
		HouseAttrKeyEntity houseAttrKey = houseAttrKeyService.getOneByTypeId(typeId);
        List<AttrKey> modelList = JSON.parseObject(houseAttrKey.getAttrKey(), new TypeReference<List<AttrKey>>() {
        });
        List<AttrValue> collect = modelList.stream().map(item -> {
            AttrValue attrValue = new AttrValue();
            attrValue.setGroup(item.getGroup());
            List<Attrs> attrs = item.getParams().stream().map(item2 -> {
                Attrs attr = new Attrs();
                attr.setAttrName(item2);
                return attr;
            }).collect(Collectors.toList());
            attrValue.setParams(attrs);
            return attrValue;
        }).collect(Collectors.toList());
        return R.ok().put("modelList", collect);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody HouseAttrKeyEntity houseAttrKey){
		houseAttrKeyService.save(houseAttrKey);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody HouseAttrKeyEntity houseAttrKey){
		houseAttrKeyService.updateById(houseAttrKey);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] attrGroupIds){
		houseAttrKeyService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
